package com.juzipi.demo.inversion.improve;

public class DependencyPass {
    public static void main(String[] args) {
        //方式一
        /*ChangHong changHong = new ChangHong();
        OpenAndClose openAndClose = new OpenAndClose();
        //传入 changHong ，因为changHong实现了ITV接口
        openAndClose.open(changHong);*/



        //方式二
        //通过构造器进行依赖的传递
        /*ChangHong changHong = new ChangHong();
        OpenAndClose openAndClose = new OpenAndClose(changHong);
        openAndClose.open();*/


        //方式三
        //通过setter方法进行依赖传递
        ChangHong changHong = new ChangHong();
        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.setItv(changHong);
        openAndClose.open();


    }

}

//方式一：通过接口传递实现依赖
//开关的接口
/*interface IOpenAndClose {
    public void open(ITV itv);//抽象方法，接收接口
}

interface ITV{
    public void play();
}

class ChangHong implements ITV{

    @Override
    public void play() {
        System.out.println("长虹电视机，打开了");
    }
}

//实现接口
class OpenAndClose implements IOpenAndClose{
    @Override
    public void open(ITV itv) {
        itv.play();
    }
}*/



//方式二: 通过构造方法传递依赖
/*interface IOpenAndClose{
    public void open();//抽象方法
}

interface ITV{
    public void play();
}

class ChangHong implements ITV{

    @Override
    public void play() {
        System.out.println("长虹电视机打开啦！");
    }
}

class OpenAndClose implements IOpenAndClose{

    public ITV itv;
    public OpenAndClose(ITV itv) {
        this.itv = itv;
    }

    @Override
    public void open() {
        this.itv.play();
    }

}*/



//方式三，通过setter方法
interface IOpenAndClose{
    public void open();
    public void setItv(ITV itv);
}

interface ITV{
    public void play();
}

class ChangHong implements ITV{

    @Override
    public void play() {
        System.out.println("打开长虹电视机啦！！！！！");
    }
}

class OpenAndClose implements IOpenAndClose{

    private ITV itv;

    @Override
    public void open() {
        this.itv.play();
    }

    @Override
    public void setItv(ITV itv) {
        this.itv = itv;
    }
}
